package com.szlanyou.tcp_server_demo.adapter;

import android.os.SystemClock;
import android.view.View;

import com.szlanyou.protocol_service.LogUtil;
import com.szlanyou.protocol_service.ProtocolHandler;
import com.szlanyou.protocol_service.Singleton;
import com.szlanyou.tcpservermaster.TcpClientItem;

import java.util.Random;

/**
 * @author 陈章
 * create at 2021/1/20 15:46
 * desc:
 */
public class ClientItemModel {
    private boolean activated = false;
    public TcpClientItem tcpClient;
    public StringBuffer cmdSb = new StringBuffer();
    public Singleton<ProtocolHandler> protocolHandlerSingleton = new Singleton<ProtocolHandler>() {
        @Override
        protected ProtocolHandler create() {
            return new ProtocolHandler();
        }
    };
    private OnViewChangedListener onViewChangedListener;


    private abstract class MyViewClickListener implements View.OnClickListener {
        public ClientItemModel data;

        public MyViewClickListener(ClientItemModel data) {
            this.data = data;
        }
    }

    public MyViewClickListener sendMsgClickListener = new MyViewClickListener(this) {
        @Override
        public void onClick(View v) {
            String cmd = "#{我是服务端" + new Random().nextInt(100) + "}#";
            data.tcpClient.send(cmd);
            data.refreshCmds(">>发送消息：" + cmd);
        }
    };


    public MyViewClickListener pressureTestClickListener = new MyViewClickListener(this) {
        @Override
        public void onClick(View v) {
            new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        String cmd = "#{我是服务端index = " + i + "}#";
                        data.tcpClient.send(cmd);
                        data.refreshCmds(">>发送消息：" + cmd);
                        SystemClock.sleep(10);
                    }
                }
            }.start();
        }
    };

    public MyViewClickListener clearLogClickListener = new MyViewClickListener(this) {
        @Override
        public void onClick(View v) {
            data.clearCmd();
        }
    };


    public ClientItemModel() {
        protocolHandlerSingleton.get().setOnDecodeListener(new ProtocolHandler.OnDecodeListener() {
            @Override
            public void onDecoded(final String oneFrameData) {
                refreshCmds("<< 收到客户端" + tcpClient.getAddress() + "消息：" + oneFrameData);
            }

            @Override
            public void onReadException(Throwable e) {
                LogUtil.e("读取流异常");
                e.printStackTrace();
            }
        });
    }

    public void clearCmd(){
        cmdSb.setLength(0);
        if(onViewChangedListener != null){
            onViewChangedListener.onViewChanged();
        }
    }

    public void refreshCmds(String cmd){
        cmdSb.append(cmd);
        cmdSb.append("\n");
        if(onViewChangedListener != null){
            onViewChangedListener.onViewChanged();
        }
    }




    public void setOnViewChangedListener(OnViewChangedListener onViewChangedListener) {
        this.onViewChangedListener = onViewChangedListener;
    }

    public interface OnViewChangedListener{
        /**
         * UI发生变化，请求列表刷新。
         */
        void onViewChanged();
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
        if(onViewChangedListener != null){
            onViewChangedListener.onViewChanged();
        }
    }

    public boolean isActivated() {
        return activated;
    }

    public TcpClientItem getTcpClient() {
        return tcpClient;
    }

    public void setTcpClient(TcpClientItem tcpClient) {
        this.tcpClient = tcpClient;
        if(onViewChangedListener != null){
            onViewChangedListener.onViewChanged();
        }
    }



}
